package day5;


import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;


public class HomeWork {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
        //demo1();

        /*Food f1 = new Food("苹果",6.0,"陕西");
        Food f2 = new Food("梨",0.5,"北京");
        demo2(f1,f2, "type");
        //demo2(f1,f2, "price");
        //demo2(f1,f2, "adds");
        System.out.println(f1);
        System.out.println(f2);*/

        demo22();
    }

    //a.使用反射获取String类的所有方法名  并且 打印方法总数  同时 还要显示按字母排序后的前5个方法名
    public static void demo1() {
        Class c = String.class;
        Method[] ms = c.getDeclaredMethods();
        int count = ms.length;
        String[] s = new String[count];
        for (int i = 0; i < ms.length; i++) {
            s[i] = ms[i].getName();
        }
        //对象比较器:
        // Comparator 目的是用于告诉集合或者数组按照什么属性进行排序
        //基本数据类型和String 一般都可以默认排序
        Arrays.sort(s);
        for (int i = 0; i < 5; i++) {
            System.out.println(s[i]);
        }
        System.out.println(count);

        /*Comparator<Method> com  = ((o1, o2) -> {
            return o1.getName().compareTo(o2.getName());
        });*/
    }


    //b.通过反射编写一个通用方法实现对象属性赋值功能，能够将一个对象的
    //属性值复制到另一个对象(同名属性)

    //同一个类的复制
    public static void demo2(Object o1, Object o2, String type) {
        try {
            Class c1 = Class.forName("day5.Food");
            Class c2 = Class.forName("day5.Food");
            Field type1 = c1.getDeclaredField(type);
            Field type2 = c2.getDeclaredField(type);
            type1.setAccessible(true);
            type2.setAccessible(true);
            Object value1 = type1.get(o1);
            type2.set(o2, value1);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    //不同类的复制
    public static void demo22() throws IllegalAccessException {
        Target1 t1 = new Target1();
        t1.a = "java";
        t1.b = "mysql";
        t1.c = "spring";
        Target2 t2 = new Target2();
        copy(t1, t2);
        System.out.println(t1);
        System.out.println(t2);
    }

    public static void copy(Object o1, Object o2) throws IllegalAccessException {
        Class c1 = o1.getClass();
        Class c2 = o2.getClass();
        Field[] ms1 = c1.getDeclaredFields();
        Field[] ms2 = c2.getDeclaredFields();
        for (Field m1 : ms1) {
            for (Field m2 : ms2) {
                if (m1.getName().equals(m2.getName()) && m1.getType() == m2.getType()) {
                    m2.set(o2, m1.get(o1));
                }
            }
        }

    }
}

class Target1 {
    String a;
    String b;
    String c;

    @Override
    public String toString() {
        return "Target1{" + "a='" + a + '\'' + ", b='" + b + '\'' + ", c='" + c + '\'' + '}';
    }
}

class Target2 {
    String b;   //类型名称都一样
    Integer c;  //类型不一样 名称一样
    String d;

    @Override
    public String toString() {
        return "Target2{" + "b='" + b + '\'' + ", c=" + c + ", d='" + d + '\'' + '}';
    }
}
